package org.example.demo.apiv2.sql;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectItem;
import org.example.demo.apiv2.constant.CommonConstant;
import org.example.demo.apiv2.entity.JoinEntity;
import org.example.demo.apiv2.entity.QuerySqlEntity;

import java.util.List;

@Slf4j
public class QuerySqlParser {

    /**
     *
     * @param queryEntity
     * @return
     */
    public static PlainSelect getSelect(QuerySqlEntity queryEntity){
        PlainSelect plainSelect = new PlainSelect();

        // 主表
        Table mainTable = TableParser.getTable(queryEntity.getSourceTable(), queryEntity.getSourceTableAlias(), CommonConstant.useAs);
        plainSelect.setFromItem(mainTable);

        // JOIN条件
        List<Join> joinList = JoinParser.getJoinList(queryEntity.getJoins());
        plainSelect.setJoins(joinList);

        // 行数
        Long limitNumber = queryEntity.getLimit();
        plainSelect.setLimitBy(LimitParser.getLimit(limitNumber));

        // WHERE条件
        Expression whereExpression = ExpressionParser.getFilterExpression(queryEntity.getFilters());
        plainSelect.setWhere(whereExpression);

        // 分组条件
        List<Column> groupByColumnList = GroupParser.getGroupByColumnList(queryEntity.getGroups());
        for (Column column : groupByColumnList) {
            plainSelect.addGroupByColumnReference(column);
        }

        // 排序条件
        List<OrderByElement> orderByElementList = OrderParser.getOrderByElementList(queryEntity.getOrders());
        plainSelect.addOrderByElements(orderByElementList);

        // 设置SelectItem
        // 主表的列
        List<SelectItem<?>> selectColList = FieldParser.getSelectItemList(queryEntity.getFields());
        plainSelect.addSelectItems(selectColList);
        // 聚合函数的列
        List<SelectItem<?>> selectAggregateColList = AggregateParser.getAggregateSelectItemList(queryEntity.getAggregates());
        plainSelect.addSelectItems(selectAggregateColList);
        // 关联表的列
        for (JoinEntity join : queryEntity.getJoins()) {
            List<SelectItem<?>> selectJoinTableColList = FieldParser.getSelectItemList(join.getFields());
            plainSelect.addSelectItems(selectJoinTableColList);
        }
        // 自定义的列
        List<SelectItem<?>> customColumnsList = CustomColumnParser.getSelectItemList(queryEntity.getCustomColumns());
        plainSelect.addSelectItems(customColumnsList);
        return plainSelect;
    }

    public static void main(String[] args) {
        // 从资源目录中读取 param.json 文件
        String jsonContent = ResourceUtil.readUtf8Str("param1.json");
//        String jsonContent = ResourceUtil.readUtf8Str("param2.json");
        QuerySqlEntity sqlEntity = JSONUtil.toBean(jsonContent, QuerySqlEntity.class);
        System.out.println(sqlEntity);
        PlainSelect sql = getSelect(sqlEntity);
        System.out.println(sql);
    }
}
